﻿using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sharp.Interprocess.MemoryManager
{
    [ProtoContract(AsReferenceDefault = true)]
    public class Segment : IEquatable<Segment>,IComparable<Segment>
    {
        [ProtoMember(1, DataFormat = DataFormat.FixedSize)]
        public int SegmentSize { get; set; }

        [ProtoMember(2, DataFormat = DataFormat.FixedSize)]
        public long SegmentOffset { get; set; }

        [ProtoMember(3, DataFormat = DataFormat.FixedSize)]
        public int SegmentPartId { get; set; }

        #region Object Overrides

        public override int GetHashCode()
        {
            unchecked
            {
                int hashCode = 17;
                hashCode = 23 * hashCode + SegmentSize.GetHashCode();
                hashCode = 23 * hashCode + SegmentOffset.GetHashCode();
                hashCode = 23 * hashCode + SegmentPartId.GetHashCode();
                return hashCode;
            }
        }

        public override bool Equals(object obj)
        {
            var otherSegment = obj as Segment;
            if (otherSegment == null) return false;

            return Equals(otherSegment);
        }

        #endregion

        #region Constructor(s)
        public Segment() {  }

        public Segment(int segmentSize, long segmentOffset)
        {
            SegmentSize = segmentSize;
            SegmentOffset = segmentOffset;
            SegmentPartId = 0;
        }
        #endregion

        #region IEquatable<Segment> Implementation
        public bool Equals(Segment other)
        {
            return other.SegmentOffset == SegmentOffset && other.SegmentSize == SegmentSize;
        }
        #endregion

        #region IComparable Implementation

        public int CompareTo(Segment other)
        {
            if (this.Equals(other)) return 0;
            if (SegmentSize > other.SegmentSize) return 1;
            else return -1;
        }

        #endregion
    }
}
